/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;

import Entity.Contact;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateless
public class ContactSessionBean implements ContactSessionBeanLocal {
    @PersistenceContext(unitName = "EasyTickets-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }
    
     @Override
    public List<Contact> viewAllContact() {
        List<Contact> list = em.createNamedQuery("Contact.findAll").getResultList();
        return list;
    }

    @Override
    public void addContact(Contact au) {
        em.persist(au);
    }

    @Override
    public void updateContact(int id, String firstName,String yourEmail,String mess,Date ctDate,boolean status) {

        Contact con = em.find(Contact.class, id);
           con.setFirstName(firstName);
           con.setYourEmail(yourEmail);
           con.setMessage(mess);
           con.setCtDate(ctDate);
           con.setStatus(status);
        em.persist(con);

    }

    @Override
    public void deleteContact(Contact fa) {
        em.remove(em.merge(fa));
    }
    public List<Contact> searchContact(String firstName) {
        List<Contact> ad = null;

        ad = em.createQuery("SELECT a FROM Contact a WHERE a.firstName like :firstName").setParameter("firstName", "%" + firstName + "%").getResultList();
        System.out.println("firstName : " + firstName);


        return ad;
    }
}
